<template>
    <div>
        <div class="po" @click="home()">返回首页</div>
        <div class="login">
            <div>
                <h1>欢迎登录叮咚！</h1>
            </div>
            <div @keyup.enter="submit()">
                <div class="username">
                    <label>
                        <span>用户名：</span
                        ><input type="text" v-model.trim="form.username" />
                    </label>
                </div>
                <div class="password">
                    <label>
                        密码：<input
                            type="password"
                            v-model.trim="form.password" />
                    </label>
                </div>
                <div class="handle">
                    <button
                        @click="submit()"
                        class="submit"
                        @keyup.enter="submit()">
                        提交
                    </button>
                    <span>
                        没有账号？
                        <!-- <span @click="register()">请注册！</span> -->
                        <router-link to="/register" tag="a"
                            >请注册！</router-link
                        >
                    </span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { login } from "@/api/request";
export default {
    data() {
        return {
            form: {
                username: "",
                password: "",
            },
        };
    },
    methods: {
        home() {
            this.$router.push("/");
        },
        submit() {
            if (this.form.username && this.form.password) {
                login("/login", this.form).then((res) => {
                    let { data } = res;
                    console.log(data);
                    if (data.code == 200) {
                        let to = this.$route.query.to;
                        localStorage.setItem("token", data.token);
                        if (to) {
                            this.$router.replace(to);
                        } else {
                            this.$router.replace('/');
                        }
                    } else {
                        this.form.username = "";
                        this.form.password = "";
                        alert("账号或密码不正确！");
                    }
                });
            } else {
                alert("账号或密码不能为空！")
            }
        },
        register() {
            
            console.log(this.$route.fullPath);
        }
    },
};
</script>

<style lang="scss" scoped>
.po {
    position: fixed;
    top: 0;
    height: 0.5rem;
    background-color: rgb(6, 193, 176);
    color: #fff;
    // width: 1.5rem;
    padding: 0.1rem;
    text-align: center;
    line-height: 0.5rem;
}
.login {
    // transform: translate(0, 50%);
    width: 80%;
    margin: 50% auto;
    // border: 1px solid #000;
    h1 {
        text-align: center;
        color: rgb(6, 193, 176);
    }
    .username label,
    .password label {
        // width: 100%;
        justify-content: space-between;
        display: flex;
        font-size: 0.4rem;
        align-items: center;
        input {
            width: 60%;
            margin: 0.18rem 0.15rem;
            border-radius: .2rem;
            
            border-color: rgb(6, 193, 176);
            &.focus {
                outline: none;
                // border: 1px   solid rgb(6, 193, 176);
            }
        }
    }
    .handle {
        margin-top: 0.2rem;
        font-size: 0.4rem;

        .submit {
            border-radius: 0.4rem;
            border: none;
            color: #fff;
            width: 1.5rem;
            height: 1rem;
            background-color: rgb(6, 193, 176);
        }
        a {
            color: rgb(6, 193, 176);
        }
    }
}
</style>
